package com.lgc.webtest.common;

import com.lgc.webtest.entity.dto.ResponseDto;
import com.lgc.webtest.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class AopHandler {

    @Around(value = ("@annotation(requestMapping)"))
    public Object recordCallLog(ProceedingJoinPoint pjp, RequestMapping requestMapping) throws Throwable {
        long start = System.currentTimeMillis();

        String params = "";//多个参数时出错 JsonUtil.toJson(pjp.getArgs());

        Object[] args = pjp.getArgs();
        Object[] arguments  = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
                //ServletRequest不能序列化，从入参里排除，否则报异常：java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
                //ServletResponse不能序列化 从入参里排除，否则报异常：java.lang.IllegalStateException: getOutputStream() has already been called for this response
                continue;
            }
            arguments[i] = args[i];
        }
        if (arguments != null) {
            try {
                params = JsonUtil.toJson(arguments);
            } catch (Exception e) {
                params = arguments.toString();
            }
        }

//        Signature signature = pjp.getSignature();
//        String clazz = signature.getDeclaringTypeName();
//        String method = signature.getName();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String path = request.getRequestURL().toString();

        log.info(">>>>>>:" + path + "," + params);

        Object result = pjp.proceed();


        String json = "";
        if(null!=result){
            json = JsonUtil.toJson(result);
        }
        long usedTime = System.currentTimeMillis() - start;

        //log.info("<<<<<<:" + method + "|耗时:" + usedTime + "毫秒");
        log.info("<<<<<< 结束请求:" + path + "|耗时:" + usedTime + "毫秒|响应:" + json );

        return result;
    }

    //@Around("execution(* set*(..))") //任何一个名字以“set”开始的方法的执行
    @Around("execution(* com.lgc.webtest.service.impl..*.*(..))")
    public Object recordCallLog1(ProceedingJoinPoint pjp) throws Throwable {
        //log.info("调用dao之前的环绕拦截1 " + System.currentTimeMillis());
        Object result = pjp.proceed();
        //log.info("调用dao之后的环绕拦截1 " + System.currentTimeMillis());

        return result;
    }
}
